package com.atguigu.lease.web.app.controller.agreement;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Package: com.atguigu.lease.web.app.controller.agreement
 *
 * @Description:
 * @Author xiongkunYang
 * @Create 2025/4/23 9:18
 */
@Tag(name = "租约信息")
@RestController
@RequestMapping("/app/agreement")
@RequiredArgsConstructor
public class LeaseAgreementController {

    private final LeaseAgreementService leaseAgreementService;

    @Operation(summary = "获取个人租约基本信息列表")
    @GetMapping("listItem")
    public Result<List<AgreementItemVo>> listItem() {
        return Result.ok(this.leaseAgreementService
                .agreementItemVoListByPhone(LoginUserContext.getLoginUser().getUsername()));
    }

    @Operation(summary = "根据id获取租约详细信息")
    @GetMapping("getDetailById")
    public Result<AgreementDetailVo> getDetailById(@RequestParam Long id) {
        return Result.ok(this.leaseAgreementService.getDetailVoById(id));
    }

    @Operation(summary = "根据id更新租约状态", description = "用于确认租约和提前退租")
    @PostMapping("updateStatusById")
    public Result<Boolean> updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus leaseStatus) {
        return Result.ok(this.leaseAgreementService.update(new LambdaUpdateWrapper<LeaseAgreement>()
                .eq(LeaseAgreement::getId, id)
                .set(LeaseAgreement::getStatus, leaseStatus)));
    }

    @Operation(summary = "保存或更新租约", description = "用于续约")
    @PostMapping("saveOrUpdate")
    public Result<Boolean> saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
        return Result.ok(this.leaseAgreementService.saveOrUpdate(leaseAgreement));
    }
}